import {SDKAppID} from './globalApi.js'

const Im = uni.requireNativePlugin("RY-TencentIM");

var msgLi=[];

//登录计算会话未读数
function reckonTabBarBadge(){
	Im.getConversationList({
		nextSeq: 0,
		count: 100
	}, res => {
		console.log('获取会话列表',res)
		msgLi=res.conversationList?res.conversationList:[];
		let num=0;
		for(var r=0;r<msgLi.length;r++){
			num+=msgLi[r].unreadCount;
		}
		if(num==0){
			uni.removeTabBarBadge({
				index:2
			})
		}else{
			uni.setTabBarBadge({
			  index: 2,
			  text: String(num)
			})
		}
	});
}
//监听会话计算：会话未读数方法
function countBadge(ret){
	let msgList=ret.conversationList?ret.conversationList:[];
	for(var i=0;i<msgList.length;i++){
		for(var r=0;r<msgLi.length;r++){
			if(msgList[i].userId==msgLi[r].userId){
				msgLi[r].unreadCount=msgList[i].unreadCount
			}
		}
	}
	let num=0;
	for(var r=0;r<msgLi.length;r++){
		num+=msgLi[r].unreadCount;
	}
	if(num==0){
		uni.removeTabBarBadge({
			index:2
		})
	}else{
		uni.setTabBarBadge({
		  index: 2,
		  text: String(num)
		})
	}
}


// 计算会话未读数并保存最新会话列表到msgLi
const setMsgLi=()=>{
	Im.getConversationList({
		nextSeq: 0,
		count: 100
	}, res => {
		console.log('获取会话列表',res)
		msgLi=res.conversationList?res.conversationList:[];
		let num=0;
		for(var r=0;r<msgLi.length;r++){
			num+=msgLi[r].unreadCount;
		}
		if(num==0){
			uni.removeTabBarBadge({
				index:2
			})
		}else{
			uni.setTabBarBadge({
			  index: 2,
			  text: String(num)
			})
		}
	});
}
// 监听会话列表，外面传入方法technique
const setConversationListener=(technique=function(){})=>{
	Im.setConversationListener({}, ret => {
		var pages = getCurrentPages();
		var page = pages[pages.length - 1].route;
		console.log(11111,page);
		countBadge(ret);
		console.log('监听会话列表',ret)
		if(page=='pages/message/message'){
			uni.$emit('setConversationListener-message',ret);
		}else if(page=='pages/HM-chat/HM-chat'){
			uni.$emit('setConversationListener-HM-chat',ret);
		}else if(page=='pages/order/pay-success'){
			uni.$emit('setConversationListener-pay-success',ret);
		}
		technique(ret);
	});
}

const init=()=>{
	Im.init({
		sdkAppId: SDKAppID(),
		docPath: plus.io.convertLocalFileSystemURL('_doc')
	}, ret => {
		console.log('IM初始化',ret)
		let res=uni.getStorageSync('user');
		console.log(res)
		if(res.token){
			Im.getLoginStatus({}, ret => {
				console.log('IM登录状态',ret)
				if(ret.loginStatus=="2"){
					Im.login({
					    userId: res.member.id + '',
					    userSig: res.sin
					}, (data) => {
						console.log('IM登录',data)
						reckonTabBarBadge()
						setConversationListener()
						setSelfInfo()
					})
				}else{
					reckonTabBarBadge()
					setConversationListener()
					setSelfInfo()
				}
			});
		}
		// uni.showToast({
		// 	title: JSON.stringify(ret),
		// 	icon: "none"
		// })
	});
}
const login=(res)=>{
	Im.getLoginStatus({}, ret => {
		console.log('IM登录状态',ret)
		if(ret.loginStatus=="2"){
			Im.login({
			    userId: res.userId + '',
			    userSig: res.userSig
			}, (data) => {
				console.log('IM登录',data)
				reckonTabBarBadge()
				setConversationListener()
				setSelfInfo();
			})
		}
	});
}
const getLoginUser=(res)=>{
	Im.getLoginUser({}, ret => {
		console.log('IM登录用户',ret)
	});
}
function setSelfInfo(){
	let member=uni.getStorageSync('user');
	console.log('修改个人资料',member);
	Im.setSelfInfo({
		selfSignature: '',
		nickName: member.member.nickname!=''?member.member.nickname:'用户'+member.member.id,
		faceUrl:member.member.avatar!=''?member.member.avatar:''
	}, ret => {
	    console.log('修改个人资料',ret);
	});
}

export default{
	Im,
	init,
	login,
	getLoginUser,
	setMsgLi,
	reckonTabBarBadge,
	setConversationListener
}

